import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/module/match/detail/zq_match_data_logic.dart';
import 'package:ball_lion/module/match/detail/zq_match_detail_logic.dart';
import 'package:ball_lion/entity/match/zq_match_data_entity.dart';
import 'package:ball_lion/entity/match/zq_match_point_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/date_time_utils.dart';
import 'package:ball_lion/utils/mode_config.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/bottom_picker_widget.dart';
import 'package:ball_lion/widgets/base/wqs_button.dart';
import 'package:ball_lion/widgets/loading/no_data_widget.dart';

import 'package:ball_lion/widgets/base/wqs_text.dart';
import 'package:ball_lion/widgets/ads_label.dart';

class ZqMatchDataView extends StatefulWidget {
  const ZqMatchDataView({super.key, this.matchId});

  final int? matchId;

  @override
  State<ZqMatchDataView> createState() => _ZqMatchDataViewState();
}

class _ZqMatchDataViewState extends State<ZqMatchDataView>
    with AutomaticKeepAliveClientMixin {
  // final tag = DateTime.now().formatedString("HH:mm:ss");
  late ZqMatchDataLogic logic;
  late final matchDetailLogic;

  @override
  void initState() {
    super.initState();
    logic = Get.put(ZqMatchDataLogic(), tag: '${widget.matchId}');
    logic.matchId = "${widget.matchId}";
    matchDetailLogic = Get.find<ZqMatchDetailLogic>(tag: '${widget.matchId}');
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return GetBuilder<ZqMatchDataLogic>(
      tag: '${widget.matchId}',
      builder: (_) {
        return logic.firstInit
            ? Container()
            : (logic.isEmpty()
                ? const NoDataWidget()
                : ListView(
                    physics: const ClampingScrollPhysics(),
                    padding: EdgeInsets.zero,
                    children: [
                      const AdsLabel(
                        adsTag: "match_zq_数据",
                        tapType: 'ggw_bsxq_data',
                        margin: EdgeInsets.fromLTRB(8, 8, 8, 0),
                      ),
                      if (logic.standingList1 != null ||
                          logic.standingList2 != null ||
                          logic.standingCupList1 != null ||
                          logic.standingCupList2 != null) ...[
                        const SizedBox(height: 8),
                        jiFenPaiMing()
                      ],
                      if (logic.data?.vsList != null &&
                          logic.data?.vsList?.length != 0)
                        Styles.card(
                            margin: const EdgeInsets.only(
                                left: 8, right: 8, top: 8),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                titleWidget('两队交锋', 'match_data_jiaofeng.png'),
                                liangDuiJiaoFeng(0),
                              ],
                            )),
                      if (logic.data?.homeVsList != null ||
                          logic.data?.guestVsList != null)
                        Styles.card(
                            margin: const EdgeInsets.only(
                                left: 8, right: 8, top: 8),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                titleWidget('近期战绩', 'match_data_recent.png'),
                                if (logic.data?.homeVsList != null)
                                  liangDuiJiaoFeng(1),
                                if (logic.data?.guestVsList != null)
                                  liangDuiJiaoFeng(2),
                              ],
                            )),
                      if (logic.data?.homeMatchList.hasValue == true ||
                          logic.data?.guestMatchList.hasValue == true)
                        Styles.card(
                            margin: const EdgeInsets.only(
                                left: 8, right: 8, top: 8),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                titleWidget('近期比赛', 'match_data_future.png'),
                                if (logic.data?.homeMatchList != null)
                                  jinQiBiSai(0),
                                if (logic.data?.guestMatchList != null)
                                  jinQiBiSai(1),
                              ],
                            )),
                      const SizedBox(height: 50)
                    ],
                  ));
      },
    );
  }

  Widget jiFenPaiMing() {
    return Styles.card(
      margin: const EdgeInsets.symmetric(horizontal: 8),
      child: Column(
        children: [
          if ((logic.standingList1 != null || logic.standingList2 != null) &&
              (logic.standingCupList1 != null ||
                  logic.standingCupList2 != null))
            Container(
              color: Colours.white,
              padding: const EdgeInsets.fromLTRB(12, 16, 12, 0),
              child: Row(
                children: [
                  Image.asset(Utils.getImgPath('match_data_point.png'),
                      width: 24),
                  const SizedBox(width: 2),
                  QsText('积分排名', fontWeight: FontWeight.w500, fontSize: 16),
                  const Spacer(),
                  if ((logic.standingList1 != null ||
                          logic.standingList2 != null) &&
                      (logic.standingCupList1 != null ||
                          logic.standingCupList2 != null))
                    Container(
                      // constraints: const BoxConstraints(maxWidth: 53,maxHeight: 23),
                      decoration: BoxDecoration(
                          color: Colours.greyF7F7F7,
                          borderRadius: BorderRadius.circular(15)),
                      child: Row(children: [
                        GestureDetector(
                          onTap: () => logic.changePointType(0),
                          child: Container(
                            height: 24,
                            width: 56,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(12),
                                color: logic.pointType == 0
                                    ? Colours.main
                                    : Colours.transparent),
                            child: QsText("杯赛",
                                fontSize: 12,
                                color: logic.pointType == 0
                                    ? Colours.white
                                    : Colours.grey66),
                          ),
                        ),
                        GestureDetector(
                          onTap: () => logic.changePointType(1),
                          child: Container(
                            height: 24,
                            width: 56,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(12),
                                color: logic.pointType == 1
                                    ? Colours.main
                                    : Colours.transparent),
                            child: QsText("联赛",
                                fontSize: 12,
                                color: logic.pointType == 1
                                    ? Colours.white
                                    : Colours.grey66),
                          ),
                        ),
                      ]),
                    )
                ],
              ),
            ),
          if (logic.pointType == 0 && logic.standingCupList1 != null)
            qiuDuiJiFen(logic.standingCupList1!, true),
          if (logic.pointType == 0 && logic.standingCupList2 != null)
            qiuDuiJiFen(logic.standingCupList2!, false),
          if (logic.pointType == 1 && logic.standingList1 != null)
            qiuDuiJiFen(
                logic.leagueHomeType == 0
                    ? logic.standingList1!
                    : (logic.leagueHomeType == 1
                        ? logic.standingHomeList1!
                        : logic.standingHomeList2!),
                true),
          if (logic.pointType == 1 && logic.standingList2 != null)
            qiuDuiJiFen(
                logic.leagueGuestType == 0
                    ? logic.standingList2!
                    : (logic.leagueGuestType == 1
                        ? logic.standingGuestList1!
                        : logic.standingGuestList2!),
                false),
        ],
      ),
    );
  }

  Widget qiuDuiJiFen(List<ZqPointList> list, bool isHome) {
    List title = ['球队', '已赛', '胜/平/负', '进/失', '净', '积分', '排名'];
    List<String> type = ['全部', '主场', '客场'];
    // final type1 = ['全部', '主场', '客场'];
    return Container(
      color: Colours.white,
      padding: EdgeInsets.fromLTRB(
          12,
          ((logic.standingList1 != null && logic.standingList2 != null) ||
                  (logic.standingCupList1 != null &&
                      logic.standingCupList2 != null))
              ? 10
              : 16,
          12,
          16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              if (!((logic.standingList1 != null &&
                      logic.standingList2 != null) ||
                  (logic.standingCupList1 != null &&
                      logic.standingCupList2 != null))) ...[
                Image.asset(Utils.getImgPath('match_data_point.png'),
                    width: 24),
                const SizedBox(width: 2),
              ],
              QsText('${list.first.leagueName}${list.first.groupCn ?? ''}积分',
                  fontWeight: FontWeight.w500,
                  fontSize: ((logic.standingList1 != null &&
                              logic.standingList2 != null) ||
                          (logic.standingCupList1 != null &&
                              logic.standingCupList2 != null))
                      ? 14
                      : 16),
              const Spacer(),
              if (logic.pointType == 1)
                SizedBox(
                    height: 24,
                    width: 51,
                    child: selectButtonWidget(
                        isHome
                            ? type[logic.leagueHomeType]
                            : type[logic.leagueGuestType], () async {
                      final result = await Get.bottomSheet(BottomPickerWidget(
                        type,
                        title: '选择主客场',
                        initialIndex: isHome
                            ? logic.leagueHomeType
                            : logic.leagueGuestType,
                        eventId: 'bsxq_sj_lsjf',
                      ));
                      if (result != null) {
                        logic.changeLeagueType(result, isHome);
                      }
                    }))
            ],
          ),
          const SizedBox(height: 10),
          Table(
            columnWidths: {
              0: const FlexColumnWidth(1),
              1: const FixedColumnWidth(40),
              2: const FixedColumnWidth(68),
              3: const FixedColumnWidth(50),
              4: const FixedColumnWidth(34),
              5: const FixedColumnWidth(34),
              6: const FixedColumnWidth(34)
            },
            border: const TableBorder(
              horizontalInside: BorderSide(color: Colours.greyEE, width: 0.5),
            ),
            defaultVerticalAlignment: TableCellVerticalAlignment.middle,
            children: [
              TableRow(
                  children: List.generate(title.length, (index) {
                return TableCell(
                    child: Padding(
                        padding: const EdgeInsets.symmetric(vertical: 8),
                        child: QsText(title[index],
                            fontSize: 12,
                            textAlign: index == 0
                                ? TextAlign.left
                                : TextAlign.center)));
              })),
              ...List.generate(list.length, (index) {
                ZqPointList data = list[index];
                return TableRow(
                    children: List.generate(7, (index1) {
                  Widget widget;
                  if (index1 == 0) {
                    widget = QsText(
                      data.teamName ?? '',
                      fontSize: 12,
                      color: data.tag == 1
                          ? Colours.main
                          : (data.tag == 2 ? Colours.blue2766D6 : Colours.text),
                      maxLines: 1,
                      textAlign: TextAlign.left,
                    );
                  } else {
                    String title = '';
                    if (index1 == 1) {
                      title = '${data.totalCount}';
                    } else if (index1 == 2) {
                      title =
                          '${data.winCount}/${data.drawCount}/${data.loseCount}';
                    } else if (index1 == 3) {
                      title = '${data.getScore}/${data.loseScore}';
                    } else if (index1 == 4) {
                      title = '${data.goalDifference}';
                    } else if (index1 == 5) {
                      title = '${data.integral}';
                    } else {
                      title = '${data.rank}';
                    }
                    widget = QsText(title,
                        fontSize: 12,
                        fontFamily: 'Din',
                        color: data.tag == 1
                            ? Colours.main
                            : (data.tag == 2
                                ? Colours.blue2766D6
                                : Colours.text),
                        textAlign: TextAlign.center);
                  }
                  return Padding(
                    padding: const EdgeInsets.symmetric(vertical: 8),
                    child: widget,
                  );
                }));
              }),
            ],
          ),
          const SizedBox(height: 8),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 12),
            child: QsButton.large(
              onPressed: () {
                Utils.onEvent("bsxq_sj_lsjf", params: {"bsxq_sj_lsjf": 4});
                Get.toNamed(Routes.leagueDetail,
                    arguments: list.first.leagueId.toString(),
                    parameters: {"type": "0"});
              },
              backgroundColor: Colours.greyF7,
              height: 32,
              borderRadius: 16,
              borderSide: const BorderSide(color: Colours.greyDD, width: 0.5),
              widget: Row(mainAxisSize: MainAxisSize.min, children: [
                QsText('查看完整排名', fontSize: 12, color: Colours.grey66),
                const SizedBox(width: 6),
                Image.asset(Utils.getImgPath('arrow_right.png'),
                    width: 6, height: 10)
              ]),
              // text: '查看完整排名 ',
              // textStyle: TextStyle(fontSize: 12, color: Colours.grey66),
            ),
          )
        ],
      ),
    );
  }

  ///两队比赛数据表
  ///0 交锋
  ///1 主队战绩
  ///2 客队战绩
  Widget liangDuiJiaoFeng(int type) {
    //final title = type == 0 ? '两队交锋' : '近期战绩';
    bool sameHome = false;
    bool sameLeague = false;
    List<ZqVsList> vsList = [];
    int zhishuType = 0;
    int matchNumType = 0;
    if (type == 0) {
      vsList = logic.vsList ?? [];
      sameHome = logic.jiaofengSameHomeVs;
      sameLeague = logic.jiaofengsSameLeagueVs;
      zhishuType = logic.jiaofengType1;
      matchNumType = logic.jiaofengType2;
    } else if (type == 1) {
      vsList = logic.homeRecentVs ?? [];
      sameHome = logic.homeSameHomeRecent;
      sameLeague = logic.homeSameLeagueRecent;
      zhishuType = logic.homeRecentType1;
      matchNumType = logic.homeRecentType2;
    } else {
      vsList = logic.guestRecentVs ?? [];
      sameHome = logic.guestSameHomeRecent;
      sameLeague = logic.guestSameLeagueRecent;
      zhishuType = logic.guestRecentType1;
      matchNumType = logic.guestRecentType2;
    }
    final tableTitle = [
      '日期/赛事',
      '主队',
      '比分',
      '客队',
      if (ModeConfig.matchDetailOdds) ...['胜平负', '让球', '进球'],
      '角球',
      '红牌'
    ];
    final type1 = [
      '初指',
      '终指',
    ];
    final type2 = [
      '近6场',
      '近10场',
      '近20场',
    ];
    return Container(
      color: Colours.white,
      padding: const EdgeInsets.fromLTRB(12, 0, 12, 12),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Visibility(
            visible: type != 0,
            child: Row(
              children: [
                CachedNetworkImage(
                  width: 21,
                  height: 21,
                  imageUrl: (type == 1
                          ? matchDetailLogic.info?.baseInfo?.homeLogo
                          : matchDetailLogic.info?.baseInfo?.guestLogo) ??
                      '',
                  placeholder: (context, url) => Styles.placeholderIcon(),
                  errorWidget: (context, url, error) => Image.asset(
                    Utils.getImgPath('zq_placeholder.png'),
                    width: 21,
                    height: 21,
                  ),
                ),
                const SizedBox(width: 8),
                QsText(
                  (type == 1
                          ? matchDetailLogic.info?.baseInfo?.homeName
                          : matchDetailLogic.info?.baseInfo?.guestName) ??
                      '',
                )
              ],
            ),
          ),
          Visibility(visible: type != 0, child: const SizedBox(height: 10)),
          Row(
            children: [
              SizedBox(
                height: 24,
                width: 52,
                child: QsButton(
                  onPressed: () {
                    if (type == 0) {
                      Utils.onEvent('bsxq_sj_ldjf1',
                          params: {'bsxq_sj_ldjf1': '1'});
                      logic.jiaofengSameHomeVs = !logic.jiaofengSameHomeVs;
                    } else if (type == 1) {
                      Utils.onEvent('bsxq_sj_jqzj_z1',
                          params: {'bsxq_sj_jqzj_z1': '1'});
                      logic.homeSameHomeRecent = !logic.homeSameHomeRecent;
                    } else {
                      Utils.onEvent('bsxq_sj_jqzj_k1',
                          params: {'bsxq_sj_jqzj_k1': '1'});
                      logic.guestSameHomeRecent = !logic.guestSameHomeRecent;
                    }
                  },
                  text: '同主客',
                  textStyle: const TextStyle(
                      fontWeight: FontWeight.w400, fontSize: 12),
                  backgroundColor:
                      sameHome ? Colours.redFEECEC : Colours.greyF7,
                  foregroundColor: sameHome ? Colours.main : Colours.grey66,
                  borderSide: BorderSide(
                      color: sameHome ? Colours.main : Colours.transparent,
                      width: 0.5),
                  // radius: 6,
                ),
              ),
              const SizedBox(width: 10),
              SizedBox(
                height: 24,
                width: 52,
                child: QsButton(
                  onPressed: () {
                    if (type == 0) {
                      Utils.onEvent('bsxq_sj_ldjf1',
                          params: {'bsxq_sj_ldjf1': '2'});
                      logic.jiaofengsSameLeagueVs =
                          !logic.jiaofengsSameLeagueVs;
                    } else if (type == 1) {
                      Utils.onEvent('bsxq_sj_jqzj_z1',
                          params: {'bsxq_sj_jqzj_z1': '2'});
                      logic.homeSameLeagueRecent = !logic.homeSameLeagueRecent;
                    } else {
                      Utils.onEvent('bsxq_sj_jqzj_k1',
                          params: {'bsxq_sj_jqzj_k1': '2'});
                      logic.guestSameLeagueRecent =
                          !logic.guestSameLeagueRecent;
                    }
                  },
                  text: '同赛事',
                  textStyle: const TextStyle(
                      fontWeight: FontWeight.w400, fontSize: 12),
                  backgroundColor:
                      sameLeague ? Colours.redFEECEC : Colours.greyF7,
                  foregroundColor: sameLeague ? Colours.main : Colours.grey66,
                  borderSide: BorderSide(
                      color: sameLeague ? Colours.main : Colours.transparent,
                      width: 0.5),
                  // radius: 6,
                ),
              ),
              const Spacer(),
              if (ModeConfig.matchDetailOdds) ...[
                SizedBox(
                    height: 24,
                    width: 51,
                    child: selectButtonWidget('${type1[zhishuType]}', () async {
                      var initialIndex = 0;
                      if (type == 0) {
                        initialIndex = logic.jiaofengType1;
                      } else if (type == 1) {
                        initialIndex = logic.homeRecentType1;
                      } else {
                        initialIndex = logic.guestRecentType1;
                      }
                      final result = await Get.bottomSheet(BottomPickerWidget(
                          type1,
                          title: '选择指数类型',
                          initialIndex: initialIndex));
                      if (result != null) {
                        logic.selectZhishu(type, result);
                      }
                    })),
                const SizedBox(width: 10)
              ],
              SizedBox(
                  height: 24,
                  width: 65,
                  child: selectButtonWidget('${type2[matchNumType]}', () async {
                    var initialIndex = 0;
                    String? eventId;
                    if (type == 0) {
                      initialIndex = logic.jiaofengType2;
                      eventId = 'bsxq_sj_ldjf2';
                    } else if (type == 1) {
                      initialIndex = logic.homeRecentType2;
                      eventId = 'bsxq_sj_jqzj_z2';
                    } else {
                      initialIndex = logic.guestRecentType2;
                      eventId = 'bsxq_sj_jqzj_k2';
                    }
                    final result = await Get.bottomSheet(BottomPickerWidget(
                      type2,
                      title: '选择场数',
                      initialIndex: initialIndex,
                      eventId: eventId,
                    ));
                    if (result != null) {
                      logic.selectMatchNumType(type, result);
                    }
                  })),
            ],
          ),
          const SizedBox(height: 12),
          vsList.isEmpty
              ? Container(
                  alignment: Alignment.center,
                  padding: const EdgeInsets.symmetric(vertical: 50),
                  child: QsText('暂无近期记录', color: Colours.grey66),
                )
              : SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        if (ModeConfig.matchDetailOdds) ...[
                          matchCount(type, vsList.length),
                          const SizedBox(height: 10)
                        ],
                        Table(
                          defaultColumnWidth: const FixedColumnWidth(50),
                          columnWidths: const {
                            0: FixedColumnWidth(70),
                            1: FixedColumnWidth(80),
                            2: FixedColumnWidth(50),
                            3: FixedColumnWidth(80),
                          },
                          border: const TableBorder(
                            horizontalInside:
                                BorderSide(color: Colours.greyEE, width: 0.5),
                          ),
                          children: [
                            TableRow(
                                children:
                                    List.generate(tableTitle.length, (index) {
                              TextAlign align = TextAlign.center;
                              if (index == 0 || index == 3) {
                                align = TextAlign.left;
                              } else if (index == 1) {
                                align = TextAlign.right;
                              }
                              return TableCell(
                                  child: Padding(
                                padding: index == 0
                                    ? const EdgeInsets.fromLTRB(0, 6, 0, 6)
                                    : const EdgeInsets.symmetric(vertical: 6),
                                child: QsText(tableTitle[index],
                                    fontSize: 12, textAlign: align),
                              ));
                            })),
                            ...List.generate(vsList.length, (index) {
                              return TableRow(
                                  children: List.generate(tableTitle.length,
                                      (index1) {
                                ZqVsList vs = vsList[index];
                                Widget widget;
                                if (tableTitle[index1] == '日期/赛事') {
                                  widget = QsText(
                                    '${DateTimeUtils.formatDateString(vs.matchTime!, 'yyyy').substring(2)}/${DateTimeUtils.formatDateString(vs.matchTime!, 'MM/dd')}\n${vs.leagueName}',
                                    fontSize: 12,
                                    color: Colours.grey66,
                                    maxLines: 2,
                                    textAlign: TextAlign.left,
                                  );
                                } else if (tableTitle[index1] == '主队') {
                                  Color color = Colours.text;
                                  if (type == 1 &&
                                      logic.data?.homeId == vs.homeId) {
                                  } else if (type == 2 &&
                                      logic.data?.guestId == vs.homeId) {
                                  } else if (type == 0) {
                                  } else {
                                    color = Colours.grey66;
                                  }
                                  widget = QsText(vs.homeName ?? "",
                                      fontSize: 12,
                                      color: color,
                                      maxLines: 2,
                                      textAlign: TextAlign.right);
                                } else if (tableTitle[index1] == '比分') {
                                  widget = Column(
                                    children: [
                                      QsText('${vs.homeScore}-${vs.guestScore}',
                                          color: logic.getColor(vs.win),
                                          fontSize: 14,
                                          fontFamily: 'Din',
                                          fontWeight: FontWeight.w500),
                                      QsText(
                                          '(${vs.homeHalfScore}-${vs.guestHalfScore})',
                                          color: Colours.grey99,
                                          fontFamily: 'Din',
                                          fontSize: 11)
                                    ],
                                  );
                                } else if (tableTitle[index1] == '客队') {
                                  Color color = Colours.text;
                                  if (type == 1 &&
                                      logic.data?.homeId == vs.guestId) {
                                  } else if (type == 2 &&
                                      logic.data?.guestId == vs.guestId) {
                                  } else if (type == 0) {
                                  } else {
                                    color = Colours.grey66;
                                  }
                                  widget = QsText(vs.guestName ?? "",
                                      fontSize: 12,
                                      color: color,
                                      maxLines: 2,
                                      textAlign: TextAlign.left);
                                } else if (tableTitle[index1] == '胜平负') {
                                  widget = QsText(logic.getOddsWin(vs, type),
                                      color: logic.getColor(vs.win),
                                      fontSize: 12,
                                      fontFamily: 'Din',
                                      textAlign: TextAlign.center);
                                } else if (tableTitle[index1] == '让球') {
                                  widget = QsText(logic.getAhWin(vs, type),
                                      color: logic.getColor(vs.ah1Win),
                                      fontSize: 12,
                                      fontFamily: 'Din',
                                      textAlign: TextAlign.center);
                                } else if (tableTitle[index1] == '进球') {
                                  widget = QsText(logic.getSbWin(vs, type),
                                      color: logic.getColor(vs.sb1Win),
                                      fontSize: 12,
                                      fontFamily: 'Din',
                                      textAlign: TextAlign.center);
                                } else if (tableTitle[index1] == '角球') {
                                  widget = QsText(
                                      vs.homeCorner == null
                                          ? '-\n-'
                                          : '${vs.homeCorner! + vs.guestCorner!}\n${vs.homeCorner}-${vs.guestCorner}',
                                      color: Colours.text,
                                      fontSize: 12,
                                      fontFamily: 'Din',
                                      textAlign: TextAlign.center);
                                } else if (tableTitle[index1] == '红牌') {
                                  widget = QsText(
                                      vs.homeRed == null
                                          ? '-\n-'
                                          : '${vs.homeRed! + vs.guestRed!}\n${vs.homeRed}-${vs.guestRed}',
                                      color: Colours.text,
                                      fontSize: 12,
                                      fontFamily: 'Din',
                                      textAlign: TextAlign.center);
                                } else {
                                  widget = QsText("");
                                }
                                return TableCell(
                                    verticalAlignment:
                                        TableCellVerticalAlignment.middle,
                                    child: Padding(
                                        padding: const EdgeInsets.symmetric(
                                            vertical: 6),
                                        child: GestureDetector(
                                            onTap: () {
                                              if (vs.matchId != null &&
                                                  vs.matchId != 0) {
                                                Get.toNamed(
                                                    Routes.zqMatchDetail,
                                                    arguments: vs.matchId,
                                                    preventDuplicates: false);
                                              }
                                            },
                                            child: widget)));
                              }));
                            })
                          ],
                        ),
                      ]),
                )
        ],
      ),
    );
  }

  //0主队 1客队
  Widget jinQiBiSai(int type) {
    final title = [
      '日期/赛事',
      '主队',
      '',
      '客队',
      '相隔',
    ];
    List<ZqMatchList> list = [];
    if (type == 0) {
      list = logic.data!.homeMatchList ?? [];
    } else {
      list = logic.data!.guestMatchList ?? [];
    }
    return Visibility(
      visible: list.isNotEmpty,
      child: Container(
        color: Colours.white,
        padding: const EdgeInsets.fromLTRB(12, 0, 12, 12),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // Visibility(
            //   visible: type == 0,
            //   child: QsText('近期比赛',
            //       style: TextStyle(fontWeight: FontWeight.w500, fontSize: 16)),
            // ),
            // Visibility(visible: type == 0, child: const SizedBox(height: 17)),
            Row(
              children: [
                CachedNetworkImage(
                  width: 21,
                  height: 21,
                  imageUrl: (type == 0
                          ? matchDetailLogic.info?.baseInfo?.homeLogo
                          : matchDetailLogic.info?.baseInfo?.guestLogo) ??
                      '',
                  placeholder: (context, url) => Styles.placeholderIcon(),
                  errorWidget: (context, url, error) => Image.asset(
                    Utils.getImgPath('zq_placeholder.png'),
                    width: 21,
                    height: 21,
                  ),
                ),
                const SizedBox(width: 8),
                QsText((type == 0
                        ? matchDetailLogic.info?.baseInfo?.homeName
                        : matchDetailLogic.info?.baseInfo?.guestName) ??
                    '')
              ],
            ),
            const SizedBox(height: 12),
            Table(
              columnWidths: const {
                0: FixedColumnWidth(70),
                1: FlexColumnWidth(1),
                2: FixedColumnWidth(40),
                3: FlexColumnWidth(1),
                4: FixedColumnWidth(40)
              },
              border: const TableBorder(
                horizontalInside: BorderSide(color: Colours.greyEE, width: 0.5),
              ),
              defaultVerticalAlignment: TableCellVerticalAlignment.middle,
              children: [
                TableRow(
                    children: List.generate(title.length, (index) {
                  TextAlign align = TextAlign.center;
                  if (index == 0 || index == 3) {
                    align = TextAlign.left;
                  } else if (index == 1) {
                    align = TextAlign.right;
                  }
                  return TableCell(
                      child: Padding(
                          padding: const EdgeInsets.symmetric(vertical: 6),
                          child: QsText(title[index],
                              fontSize: 12, textAlign: align)));
                })),
                ...List.generate(list.length, (index) {
                  return TableRow(
                      children: List.generate(5, (index1) {
                    ZqMatchList vs = list[index];
                    Widget widget;
                    if (index1 == 0) {
                      widget = QsText(
                        '${DateTimeUtils.formatDateString(vs.matchTime!, 'yyyy').substring(2)}/${DateTimeUtils.formatDateString(vs.matchTime!, 'MM/dd')}\n${vs.leagueName}',
                        fontSize: 12,
                        color: Colours.grey66,
                        textAlign: TextAlign.left,
                      );
                    } else if (index1 == 1) {
                      Color color = Colours.text;
                      if (type == 0 && logic.data?.homeId == vs.homeId) {
                      } else if (type == 1 &&
                          logic.data?.guestId == vs.homeId) {
                      } else {
                        color = Colours.grey66;
                      }
                      widget = QsText(vs.homeName!,
                          fontSize: 12,
                          color: color,
                          maxLines: 2,
                          textAlign: TextAlign.right);
                    } else if (index1 == 2) {
                      widget = QsText('vs',
                          fontSize: 12, textAlign: TextAlign.center);
                    } else if (index1 == 3) {
                      Color color = Colours.text;
                      if (type == 0 && logic.data?.homeId == vs.guestId) {
                      } else if (type == 1 &&
                          logic.data?.guestId == vs.guestId) {
                      } else {
                        color = Colours.grey66;
                      }
                      widget = QsText(vs.guestName!,
                          fontSize: 12,
                          color: color,
                          maxLines: 2,
                          textAlign: TextAlign.left);
                    } else {
                      widget = QsText('${vs.days}天',
                          fontSize: 12, textAlign: TextAlign.center);
                    }
                    return GestureDetector(
                      onTap: () {
                        if (vs.matchId != null && vs.matchId != 0) {
                          Get.toNamed(Routes.zqMatchDetail,
                              arguments: vs.matchId, preventDuplicates: false);
                        }
                      },
                      child: Padding(
                        padding: const EdgeInsets.symmetric(vertical: 4),
                        child: widget,
                      ),
                    );
                  }));
                }),
              ],
            ),
            // const SizedBox(height: 20),
          ],
        ),
      ),
    );
  }

  Widget matchCount(int type, int length) {
    List goalAndLost = logic.getGoalAndLost(type);
    List winPercentage = logic.getWinPercentage(type);
    List ahPercentage = logic.getAhPercentage(type);
    List sbPercentage = logic.getDxWinPercentage(type);
    return Row(
      // mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Column(
          children: [
            Container(
                decoration: const BoxDecoration(
                    color: Colours.greyD9,
                    borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(2),
                        topRight: Radius.circular(2))),
                width: 40,
                height: 20,
                alignment: Alignment.center,
                child: QsText(type == 0 ? '主队近' : '近', fontSize: 10)),
            Container(
                decoration: BoxDecoration(
                    borderRadius: const BorderRadius.only(
                        bottomLeft: Radius.circular(2),
                        bottomRight: Radius.circular(2)),
                    border: Border.all(width: 1, color: Colours.greyD9)),
                width: 40,
                height: 20,
                alignment: Alignment.center,
                child: QsText('$length场', fontSize: 12)),
          ],
        ),
        const SizedBox(width: 12),
        Column(
          children: [
            QsText(
              '${goalAndLost[0]}/${goalAndLost[1]}',
              fontSize: 16,
              fontWeight: FontWeight.w500,
              color: Colours.main,
              fontFamily: 'Din',
            ),
            QsText(
              '进球/失球',
              fontSize: 10,
              color: Colours.grey99,
            ),
          ],
        ),
        const SizedBox(width: 16),
        Column(
          children: [
            Row(
                crossAxisAlignment: CrossAxisAlignment.baseline,
                textBaseline: TextBaseline.alphabetic,
                children: [
                  QsText(
                    '胜率 ',
                    fontSize: 12,
                    color: Colours.blackColor,
                  ),
                  QsText(
                    '${winPercentage[0]}%',
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                    color: Colours.main,
                    fontFamily: 'Din',
                  ),
                ]),
            QsText(
              '${winPercentage[1]}胜${winPercentage[2]}平${winPercentage[3]}负',
              fontSize: 10,
              color: Colours.grey99,
            ),
          ],
        ),
        const SizedBox(width: 16),
        Column(
          children: [
            Row(
                crossAxisAlignment: CrossAxisAlignment.baseline,
                textBaseline: TextBaseline.alphabetic,
                children: [
                  QsText(
                    '赢率 ',
                    fontSize: 12,
                    color: Colours.blackColor,
                  ),
                  QsText('${ahPercentage[0]}%',
                      fontSize: 16,
                      fontWeight: FontWeight.w500,
                      color: Colours.main,
                      fontFamily: 'Din'),
                ]),
            QsText(
              '${ahPercentage[1]}赢${ahPercentage[2]}走${ahPercentage[3]}输',
              fontSize: 10,
              color: Colours.grey99,
            ),
          ],
        ),
        const SizedBox(width: 16),
        Column(
          children: [
            Row(
                crossAxisAlignment: CrossAxisAlignment.baseline,
                textBaseline: TextBaseline.alphabetic,
                children: [
                  QsText(
                    '大 ',
                    fontSize: 12,
                    color: Colours.blackColor,
                  ),
                  QsText(
                    '${sbPercentage[0]}%',
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                    color: Colours.main,
                    fontFamily: 'Din',
                  ),
                ]),
            QsText(
              '${sbPercentage[1]}大${sbPercentage[2]}走${sbPercentage[3]}小',
              fontSize: 10,
              color: Colours.grey99,
            ),
          ],
        ),
      ],
    );
  }

  Widget titleWidget(String title, String image) {
    return Container(
      color: Colours.white,
      padding: const EdgeInsets.only(left: 12, top: 16, bottom: 16),
      child: Row(
        children: [
          Image.asset(Utils.getImgPath(image), width: 24),
          const SizedBox(width: 2),
          QsText(title, fontWeight: FontWeight.w500, fontSize: 16),
        ],
      ),
    );
  }

  Widget selectButtonWidget(String title, Function f) {
    return TextButton(
        style: ButtonStyle(
          padding: MaterialStateProperty.all(EdgeInsets.zero),
          side: MaterialStateProperty.all<BorderSide>(
            BorderSide(width: 0.5, color: Colours.grey66.withOpacity(0.5)),
          ),
          shape: MaterialStateProperty.all<OutlinedBorder>(
            RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12),
            ),
          ),
        ),
        onPressed: () => f(),
        child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
          QsText(
            title,
            fontSize: 12,
            color: Colours.grey66,
          ),
          const SizedBox(width: 4),
          Image.asset(
            Utils.getImgPath('down_arrow.png'),
            width: 8,
            height: 6,
          )
        ]));
  }

  @override
  bool get wantKeepAlive => true;
}
